<?php

/*===============微信商城=================*/
/*----------------------------------------*/
/* $Author: 疯中追风                      */
/*----------------------------------------*/
/* $Time: 2017/10/11 9:12                 */
/*----------------------------------------*/
/*=================首页===================*/

namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller{

    public function index(){
      /*  cookie(null);*/
        //首先判断是否只是移动端
        if(isMobile() == true){
             if(cookie('id') == null){
            redirect('https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5b3db39546bb1360&redirect_uri=http%3a%2f%2fwww.jsyuanma.top%2findex.php%2fHome%2fIndex%2fweixin_interface%2f&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect');
            }
        
        	//实例化模板
            $model = M();
        	$goods = M('Goods');
        	$goods_type = M('Goods_type');
            $goods_carousel = M('Goods_carousel');
        	$goods_type_parent = M('Goods_type_parent');

        	//查询所有分类信息
        	$types1  =  $goods_type -> where('main_type_id = 1') ->  select();
            $types2  =  $goods_type -> where('main_type_id = 2') ->  select();
            $types3  =  $goods_type -> where('main_type_id = 3') ->  select();
            $types4  =  $goods_type -> where('main_type_id = 4') ->  select();
        	$types_parent = $goods_type_parent -> select();

            //查询轮播图信息,并显示前4条信息
            $carousel = $model -> table('sp_goods as t1,sp_goods_carousel as t2') -> where('t1.id = t2.goods_id') -> order('time asc') -> limit(0,4) -> select();

            //查询新品信息,并显示前3条记录
            $new_goods_info = $goods -> order('goods_time desc') -> where('is_new=1') -> limit(0,3) -> select();

        	//查询热销商品,只显示前6条记录
        	$hot_goods = $goods -> where('is_hot=1') -> limit(0,6) -> order('sales desc') -> select();

    		//展示所有分类
            $this -> assign('types1',$types1);
            $this -> assign('types2',$types2);
            $this -> assign('types3',$types3);
            $this -> assign('types4',$types4);
        	$this -> assign('types_parent',$types_parent);
            //展示轮播
            $this -> assign('carousel',$carousel);
            //展示新品信息
            $this -> assign('new_goods_info',$new_goods_info);
        	//展示热销商品
        	$this -> assign('hot_goods',$hot_goods);
        	$this -> display();
        }else{
            redirect(U('Pc/Index/index'));
        }
    }

    //随机查询商品并输出首页
     public function ajaxGetMore(){
 		/*实力化模板*/
    	$goods = M('Goods');
    	//声明一个存放随机商品的一个数组
    	$love_goods = array();
    	//随机出来6个商品
    	for($i = 0;$i < 6;$i++){
    		$rand = mt_rand(1,17);
    		$love_good = $goods -> where("id = $rand") -> find();
    		$love_goods[$i] = $love_good;
    	}

    	$this -> assign('love_goods',$love_goods);
    	$this -> display();
    }

    public function  weixin_interface(){
        //首先判断session中是否存在用户名和头像。如果存在则不用使用oauth请求access_token
            $date = getdate(date("U"));
            $current_date = $date[year].'-'.($date[mon]+1).'-'.$date[mday]." "."  ".$date[hours].':'.$date[minutes].':'.$date[seconds];
            //获取code
            $code  = $_GET['code'];

            //公众号唯一id
            $appid = 'wx5b3db39546bb1360';
            $appsecret = '9ff6efc83ad1e67a07403fe402ccd1b8';

            //通过code获取access_token
            $get_access_token = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
            $text = file_get_contents($get_access_token);

            //刷新access_token
            $refresh_token = json_decode($text,true)['refresh_token'];
            $text2 = file_get_contents('https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$refresh_token);
            //将json字符串转换为数组
            $access_token =  json_decode($text2,true)['access_token'];
            //用户唯一id
            $openid       =  json_decode($text,true)['openid'];

            //获取微信个人信息
            $personInfo   =  file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');

            $name     = json_decode($personInfo,true)['nickname'];
            $imageUrl = json_decode($personInfo,true)['headimgurl'];
            //将昵称和头像地址放入到cookie里面
            cookie('name',$name,7000);
            cookie('imageUrl',$imageUrl,7000);
            //首先判断是否存在用户
            $result = M("User") -> where(array('nickname'=>$name,'head_pic'=>$imageUrl)) -> find();
            
            if(!$result){   

                //获取ip地址
                $ip = get_client_ip();

                //存入数据库
                M("User") -> add(array('oauth'=>'weixin','head_pic'=>$imageUrl,'nickname'=>$name,'last_login'=>$current_date,'last_ip'=>$ip));
                $id = M("User") -> where(array('nickname'=>$name)) -> find();
                cookie('id',$id['user_id'],7000);
                redirect(U('index'));
            }else{
                cookie('id',$result['user_id'],7000);
                redirect(U('index'));
            }
    }

    //获取签名信息
    public function  get_ticket(){
        $get_id = I('get.id');

        if(cookie('ticket2')==null){
            //获取access_token,注意；这里的access_token和通过网页获取授权的access_token是不一致的
            $json_text = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx5b3db39546bb1360&secret=9ff6efc83ad1e67a07403fe402ccd1b8");
            $access_token2 = json_decode($json_text,true)['access_token'];
            //请求地址，获取带有签名的json字符串
            $get_ticket_json = file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token2."&type=jsapi");
            //获取签名
            $ticket2 = json_decode($get_ticket_json,true)['ticket'];
            //将ticket存入session里面
            //cookie('ticket',$ticket,7000);
            //4.使用sha1()方法进行签名.
            cookie('ticket2',$ticket2,7000);
            $signature = sha1('jsapi_ticket='.$ticket2.'&noncestr=wsssssfdfdaf&timestamp=1510191818&url=http://www.jsyuanma.top/index.php/Home/GoodsInfo/goodsInfo/id/'.$get_id);
            //返回签名
            echo $signature;
        }else{
            //将ticket存入session里面
            //cookie('ticket',$ticket,7000);
            //4.使用sha1()方法进行签名.
            //判断是否带后缀".html"
            $signature = sha1('jsapi_ticket='.cookie('ticket2').'&noncestr=wsssssfdfdaf&timestamp=1510191818&url=http://www.jsyuanma.top/index.php/Home/GoodsInfo/goodsInfo/id/'.$get_id);
            //返回签名
            echo $signature;
        }
        /*
        }
PaDkb9Sg4OUxklWiRMX-XZ754SyI1p87Kk8ONUi-OQOMAQxAfG_WJm4z0ikOhdX6kuT0jv9WZD3tie_mFVILeA61xF1svs2pyJXRye1BqmQ

https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=xAo-S9cE7361jRN_WjXjc8x5G1fVzwPq6Ocs-bDqcHrJacORHxCFXMOCdyQuX94jtmPmx6Jj1UX9pyHduZnktS3UfExHyUKIZi-rYxKpAKY&type=jsapi
*/
    }

    //错误提示信息
    public function _empty(){
        echo "您访问的页面不存在！";
    }

}